package com.hp.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hp.bean.User;
import com.hp.mapper.UserMapper;
import com.mysql.jdbc.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.Resource;
import javax.management.relation.Role;
import java.util.List;

public class UserController {
    @Resource
    private UserMapper userMapper;
    @Resource
    private RedisTemplate redisTemplate;
    public String findStr(User user) {
        //取值
        String roleList = (String)redisTemplate.opsForValue().get("roleList");
        if(StringUtils.isNullOrEmpty(roleList)){
            List<Role> list = userMapper.selectByExample(user);
            ObjectMapper mapper = new ObjectMapper();
            try {
                roleList = mapper.writeValueAsString(list);
              //存值
                redisTemplate.opsForValue().set("roleList",roleList);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }else{
            System.out.println("使用缓存数据！");
        }
        return roleList;
    }
}
